 /*******************************************************************************
 * Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Author - Sebastian Zarnekow
 * See https://github.com/szarnekow/xtend-euler 
 *******************************************************************************/
 package euler

import com.google.common.collect.AbstractIterator

/**
 * Each new term in the Fibonacci sequence is generated by adding the previous two terms.
 * By starting with 1 and 2, the first 10 terms will be:
 * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
 *
 * By considering the terms in the Fibonacci sequence whose values do not exceed four million,
 * find the sum of the even-valued terms.
 *
 * @see http://projecteuler.net/problem=2
 */
class Solution_002 extends AbstractIterator<Long> {

	def static void main(String[] args) {
		println(new Solution_002().reduce[l1, l2 | l1 + l2])
	}

	long l0 = 0
	long l1 = 1

	override protected computeNext() {
		if (l1 > 4000000)
			endOfData
		var result = l0 + l1
		l0 = l1
		l1 = result
		if (result % 2 == 0)
			result
		else
			computeNext
	}

}